package edu.njfu.wigetdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import edu.njfu.wigetdemo.adapter.FruitAdapter
import edu.njfu.wigetdemo.model.Fruit
import kotlinx.android.synthetic.main.activity_list_view.*

class ListViewActivity : AppCompatActivity() {

    //private val data=listOf("Apple","Orange","Pear","Grape")
    private  val fruitList=ArrayList<Fruit>()
    private fun initFruit(){
        repeat(2){
            fruitList.add(Fruit("Apple",R.drawable.apple_pic))
            fruitList.add(Fruit("Banana",R.drawable.banana_pic))
            fruitList.add(Fruit("Orange",R.drawable.orange_pic))
            fruitList.add(Fruit("Watermelon",R.drawable.watermelon_pic))
            fruitList.add(Fruit("Pear",R.drawable.pear_pic))
            fruitList.add(Fruit("Grape",R.drawable.grape_pic))
            fruitList.add(Fruit("PineApple",R.drawable.pineapple_pic))
            fruitList.add(Fruit("StrawBerry",R.drawable.strawberry_pic))
            fruitList.add(Fruit("Cherry",R.drawable.cherry_pic))
            fruitList.add(Fruit("Mango",R.drawable.mango_pic))
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list_view)
        //val adapter =ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data)
        //listView.adapter=adapter
        initFruit()
        val adapter=FruitAdapter(this,R.layout.fruit_item_layout,fruitList)
        listView.adapter=adapter
        listView.setOnItemClickListener {

            _,_,position,_->
                val fruit=fruitList
                Toast.makeText(this,fruit.toString(),Toast.LENGTH_SHORT).show()

        }

    }
}